exchange<?php 
require_once 'ckLogin.php';
require_once 'init.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();
//auctionsuccess($db);

//$_GET['startTime'] $_GET['endTime'] $_GET['aGoodsName'] $_GET['nickName']
$where = '';
if (!empty($_GET['startTime'])) {
	$where .= " AND auctionDate>='{$_GET['startTime']}'";
}
if (!empty($_GET['endTime'])) {
	$where .= " AND auctionDate<='{$_GET['endTime']}'";
}
if (!empty($_GET['aGoodsName'])) {
	$where .= " AND name LIKE '%{$_GET['aGoodsName']}%'";
}
if (!empty($_GET['nickName'])) {
	$where .= " AND nickname LIKE '%{$_GET['nickName']}%'";
}
$pageSize = 20;
$db->sql = "SELECT COUNT(id) FROM auctionsuccess WHERE 1=1 {$where}";
$total = $db->doResultOne();
$page = isset($_GET['page'])?$_GET['page']:1;
$pageArray = page($total, $pageSize, $page);
$db->sql = "SELECT * FROM auctionsuccess WHERE 1=1 {$where} ORDER BY auctionDate DESC ,agid ASC LIMIT {$pageArray['start']},{$pageSize}";
$auction = $db->doResultList2();

/* $db->sql = "SELECT count(a.id) FROM auction a LEFT JOIN auctiongoods ag ON a.agid = ag.id WHERE a.id IN (SELECT id FROM (SELECT id,offer FROM (SELECT id,agid,auctionDate,offer,count(id) FROM auction GROUP BY agid,auctionDate,offer HAVING count(id)=1) a1 GROUP BY agid,auctionDate HAVING offer = MIN(offer)) a2)";
$total = $db->doResultOne();

$page = isset($_GET['page'])?$_GET['page']:1;
$pageArray = page($total, $pageSize, $page);
echo $db->sql = "SELECT ag.*,a.id aid,a.offer,a.auctionDate,a.nickname FROM auction a LEFT JOIN auctiongoods ag ON a.agid = ag.id WHERE a.id IN (SELECT id FROM (SELECT id,offer FROM (SELECT id,agid,auctionDate,offer,count(id) FROM auction GROUP BY agid,auctionDate,offer HAVING count(id)=1) a1 GROUP BY agid,auctionDate HAVING offer = MIN(offer)) a2) ORDER BY a.auctionDate DESC LIMIT {$pageArray['start']},{$pageSize}";
$auction = $db->doResultList2();
foreach ($auction as $key=>$item){
	$db->sql = "SELECT COUNT(id) FROM auction WHERE agid = {$item['id']} AND auctionDate = '{$item['auctionDate']}'";
	$auction[$key]['totleOffer'] = $db->doResultOne();
} */
//分页
function page($total,$size,$page){
	//最大页数
	if($total%$size==0){
		$maxPage = $total/$size;
	}else{
		$maxPage = (int)($total/$size)+1;
	}
	//开始条数
	$start = ($page-1)*$size;
	//分页
	if($maxPage>=5){
		if($page<=2){
			$pages=array(1,2,3,4,5);
		}elseif($page>=($maxPage-1)){
			$pages=array($maxPage-4,$maxPage-3,$maxPage-2,$maxPage-1,$maxPage);
		}else{
			$pages=array($page-2,$page-1,$page,$page+1,$page+2);
		}
	}else{
		for($i=1;$i<=$maxPage;$i++){
			$pages[]=$i;
		}
	}
	if($page<=1){$previous=1;}else{$previous=$page-1;}
	if($page>=$maxPage) {$next = $maxPage;}else{ $next = $page+1;}
	return array('maxPage'=>$maxPage,'start'=>$start,'pages'=>$pages,'previous'=>$previous,'next'=>$next);
}
//$db->doExecute();
//$db->sql = "SELECT COUNT(id) FROM auctionsuccess WHERE auctionDate = "2013-03-18"";
function auctionsuccess($db){
	$db->sql = "SELECT COUNT(id) FROM auctionsuccess WHERE auctionDate = '".date("Y-m-d",strtotime("-1 day"))."'";
	$total1 = $db->doResultOne();
	$db->sql = "SELECT COUNT(id) FROM (SELECT id,offer FROM (SELECT id,agid,auctionDate,offer,count(id) FROM auction WHERE auctionDate = '".date("Y-m-d",strtotime("-1 day"))."' GROUP BY agid,auctionDate,offer HAVING count(id)=1) a1 GROUP BY agid,auctionDate HAVING offer = MIN(offer)) a2";
	$total2 = $db->doResultOne();
	if ($total1!=$total2) {
		$db->sql = "DELETE FROM auctionsuccess WHERE auctionDate = '".date("Y-m-d",strtotime("-1 day"))."'";
		$db->doExecute();
		$db->sql = "INSERT INTO auctionsuccess(agid,aid,pDate,pwd,`name`,pirce,pic,url,auctionDate,auctionTime,nickname,gender,city,openid,offer) ".
					"SELECT ag.id agid, a.id aid, ag.pDate, ag.pwd, ag.name, ag.pirce,ag.pic,ag.url,a.auctionDate,a.auctionTime,a.nickname,a.gender,a.city,a.openid,a.offer FROM auction a LEFT JOIN auctiongoods ag ON a.agid = ag.id ".
					"WHERE a.id IN (SELECT id FROM (SELECT id,offer FROM (SELECT id,agid,auctionDate,offer,count(id) FROM auction WHERE auctionDate = '".date("Y-m-d",strtotime("-1 day"))."' GROUP BY agid,auctionDate,offer HAVING count(id)=1) a1 GROUP BY agid,auctionDate HAVING offer = MIN(offer)) a2) ";
		$db->doExecute();
		$db->sql = "UPDATE auctionsuccess a1 SET offertimes = (SELECT COUNT(id) FROM auction a2 WHERE a2.agid=a1.agid AND a2.auctionDate=a1.auctionDate) WHERE offertimes IS NULL";
		$db->doExecute();
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo APPLICATION;?></title>
<!--                       CSS                       -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css" media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/main.css" type="text/css" media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css" media="screen" />

<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function($) {
	//删除
	$(".deleteUser").click(function(){
		if(!confirm("是否将此用户删除?")){
			return false;
		}
	});
	//修改
	$(".editcoupon").click(function(){
		//alert(123);
		var couponName = $(this).parents("tr").children("td").eq(2).text();
		var couponID = $(this).parents("tr").children("td").eq(0).text();
		var couponUrl = $(this).parents("tr").children("td").eq(3).text();
		var couponCost = $(this).parents("tr").children("td").eq(4).text();
		$("#couponName").val(couponName);
		$("#couponUrl").val(couponUrl);
		$("#couponCost").val(couponCost);
		$("#couponID").val(couponID);

		//var activity_id = tiqu(couponUrl ,/activity_id=([0-9]+)($|&)/i);
		//var seller_id = tiqu(couponUrl ,/seller_id=([0-9]+)($|&)/i);

		//alert(activity_id+'~~'+seller_id);
		$("#message_click").click();
	});
	
	$(".success").ajaxSuccess(function(evt, request, settings){
		//$(this).fadeIn(400).delay(800).slideUp(300);
		$(this).show(400).delay(800).hide(300);
		//$(this).hide();
	});
	$(".error").ajaxError(function(event,request, settings){
		$(this).show(400).delay(800).hide(300);
	});
	$(".isreceive").change(function(){
		if(confirm("确认或取消领取吗？")){
			var id = $(this).data("id");
			
			$.ajax({
				type: "GET",
				url: "youpinAjax.php",
				data: "id="+id+"&act=isreceive",
				dataType: "html",
				success: function(date){
					
				}
			});
		}else{
			return false;
		}
	});
	$(".isInvalid").change(function(){
		if(confirm("确认或取消领取吗？")){
			var id = $(this).data("id");
			
			$.ajax({
				type: "GET",
				url: "youpinAjax.php",
				data: "id="+id+"&act=isInvalid",
				dataType: "html",
				success: function(date){
					
				}
			});
		}else{
			return false;
		}
	});
	$(".isExceedTheTimeLimit").change(function(){
		if(confirm("确认或取消领取吗？")){
			var id = $(this).data("id");
			
			$.ajax({
				type: "GET",
				url: "youpinAjax.php",
				data: "id="+id+"&act=isExceedTheTimeLimit",
				dataType: "html",
				success: function(date){
					
				}
			});
		}else{
			return false;
		}
	});
});

</script>
<!-- jQuery Configuration -->
<script type="text/javascript"
	src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>
<!-- jQuery colorbox -->
<link rel="stylesheet" href="resources/css/colorbox.css" />
<!-- jQuery my97datepicker -->
<script type="text/javascript" src="resources/scripts/My97DatePicker/WdatePicker.js"></script>
<script src="resources/scripts/colorbox/jquery.colorbox.js"></script>
<script type="text/javascript">
$(function($) {
	$(".group-pic").colorbox({rel:'group-pic',width:"30%", height:"50%"});
});
</script>
</head>

<body>
	<div id="body-wrapper">
		<!-- Wrapper for the radial gradient background -->
		<a href="#messages" rel="modal" id="message_click"></a>
		<div id="messages" style="display: none;">
			<!-- Messages are shown when a link with these attributes are clicked: href="#messages" rel="modal"  -->

			<p style="">
				<strong>简短描述输入及修改</strong>
			</p>
			<form action="?act=edit" method="post">
				<fieldset>
					标题：<input class="text-input " name="couponName" id="couponName" type="text" value="" style="width: 310px;"/>
					地址：<input class="text-input " name="couponUrl" id="couponUrl" type="text" value="" style="width: 310px;"/>
					积分：<input class="text-input " name="couponCost" id="couponCost" type="text" value="" style="width: 310px;"/>
					<input class="text-input " id="couponID" name="couponID" type="hidden" value="" style="width: 310px;"/>
					<input class="button" type="submit" value="修改" /> 
					<a href="javascript:void(0);" onclick="$(this).parents('#facebox').hide();">关闭</a>
				</fieldset>
			</form>
		</div>
		<!-- End #messages -->
		<div id="main-content">
			<!-- Page Head -->
			<h2>欢迎使用<?php echo APPLICATION;?>管理后台</h2>
			<p id="page-intro"></p>

			<div class="clear"></div>
			<!-- End .clear -->

			<div class="content-box">
				<!-- Start Content Box -->

				<div class="content-box-header">

					<h3>竞拍管理</h3>

					<div class="clear"></div>

				</div>
				<!-- End .content-box-header -->

				<div class="content-box-content">

				<!-- Start Notifications -->
				
				<div class="notification success png_bg" style="display: none;">
					<a href="#" class="close"><img
						src="resources/images/icons/cross_grey_small.png"
						title="Close this notification" alt="close" /></a>
					<div>
						修改成功
					</div>
				</div>
				
				<div class="notification error png_bg" style="display: none;">
					<a href="#" class="close"><img
						src="resources/images/icons/cross_grey_small.png"
						title="Close this notification" alt="close" /></a>
					<div>
					修改失败
					</div>
				</div>
				
				<!-- End Notifications -->
				<!-- $_GET['startTime'] $_GET['endTime'] $_GET['aGoodsName'] $_GET['nickName'] -->
					<form method="get" action="">
						时间：
						<input type="text" id="startTime" name="startTime" onClick="WdatePicker()" value="<?php echo $_GET['startTime'];?>" size="10"/> 
						-
						<input type="text" id="endTime" name="endTime" onClick="WdatePicker()" value="<?php echo $_GET['endTime'];?>" size="10"/>
						&nbsp;&nbsp;&nbsp;
						商品名称：<input id="aGoodsName" name="aGoodsName" type="text" value="<?php echo $_GET['aGoodsName'];?>"/>
						&nbsp;&nbsp;&nbsp;
						获胜者：<input id="nickName" name="nickName" type="text" value="<?php echo $_GET['nickName'];?>"/>
						&nbsp;&nbsp;&nbsp;
						<button class="button" id="zhixing" type="submit">搜索</button>
					</form>
					
					<hr />
					<div class="clear"></div>
					<div class="tab-content default-tab" id="tab1">
						<!-- This is the target div. id must match the href of this div's tab -->

						<table>
							<thead>
								<tr>
									<th>编号</th>
									<th>商品id</th>
									<th style="width: 20%;">名称</th>
									<!-- <th>旺旺</th> -->
									<th>获奖时间</th>
									<th>市场价格</th>
									<th>出价次数</th>
									<th>唯一最低价</th>
									<?php //if ($_SESSION['userId']==16 || $_SESSION['userId']==28 || $_SESSION['userId']==30 || $_SESSION['userId']==31): ?><th>密码</th><?php //endif;?>
									<th style="width: 15%;">获胜者</th>
									<th>是否<br />领取</th>
									<th>是否<br />无效</th>
									<th>是否<br />过期</th>
								</tr>

							</thead>

							<tbody>
							<?php if(count($auction)>0):?>
							<?php foreach ($auction as $key=>$item):?>
								<tr id="tr-<?php echo $item['id'];?>">
									<td><?php echo $item['id'];?></td>
									<td><?php echo $item['agid'];?></td>
									<td><?php echo $item['name'];?></td>
									<!-- <td><?php echo $item['ww'];?></td> -->
									<td><?php echo $item['auctionDate'];?></td>
									<td><?php echo $item['pirce'];?></td>
									<td><?php echo $item['offertimes'];?></td>
									<td><?php echo $item['offer'];?></td>
									<?php //if ($_SESSION['userId']==16 || $_SESSION['userId']==28  || $_SESSION['userId']==30 || $_SESSION['userId']==31): ?><td><?php echo $item['pwd'];?></td><?php //endif;?>
									<td><?php echo $item['nickname'];?></td>
									<td><input type="checkbox" class="isreceive" data-id="<?php echo $item['id'];?>" <?php if ($item['isreceive']==1) {echo 'checked="checked"';}?>/></td>
									<td><input type="checkbox" class="isInvalid" data-id="<?php echo $item['id'];?>" <?php if ($item['isInvalid']==1) {echo 'checked="checked"';}?>/></td>
									<td><input type="checkbox" class="isExceedTheTimeLimit" data-id="<?php echo $item['id'];?>" <?php if ($item['isExceedTheTimeLimit']==1) {echo 'checked="checked"';}?>/></td>
								</tr>
							<?php endforeach;?>
							<?php else:?>
								<tr style="font-size: 50px;text-align: center; font-weight: bold;"><td colspan="5" style="text-align: center;">没有竞拍结果！！</td></tr>
							<?php endif;?>
							</tbody>
							<tfoot>
								<tr>
									<td colspan="17">
										<div class="bulk-actions align-left">
											
										</div>
										<?php if($pageArray['maxPage']>0):?>
										<div class="pagination">
											<a href="?1=1&page=1" title="First Page">&laquo; First</a>
											<a href="<?php echo "?1=1&page=".$pageArray['previous'];?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pageArray['pages'] as $item):?> 
											<a href="<?php echo "?1=1&page=".$item;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="<?php echo "?1=1&page=".$pageArray['next'];?>" title="Next Page">Next &raquo;</a>
											<a href="<?php echo "?1=1&page=".$pageArray['maxPage'];?>" title="Last Page">Last &raquo;</a>
											total : <?php echo $pageArray['maxPage'];?>
										</div> 
										<?php endif;?>
										<!-- End .pagination -->
										<div class="clear"></div>
									</td>
								</tr>
							</tfoot>
						</table>

					</div>
					<!-- End #tab2 -->

				</div>
				<!-- End .content-box-content -->

			</div>
			<!-- End .content-box -->

			<div class="clear"></div>

			<div id="footer">
				<small> <!-- Remove this notice or replace it with whatever you want -->
					&#169; Copyright 2009 Your Company | Powered by <a
					href="http://themeforest.net/item/simpla-admin-flexible-user-friendly-admin-skin/46073">Simpla
						Admin</a> | <a href="#">Top</a>
				</small>
			</div>
			<!-- End #footer -->

		</div>
		<!-- End #main-content -->

	</div>
</body>


<!-- Download From www.exet.tk-->
</html>
